#!/usr/bin/python
# -*- coding: utf-8 -*-

from sshtunnel import SSHTunnelForwarder
from pymongo import MongoClient
import paramiko
import msgpack
import binascii
from bson.objectid import ObjectId

import sys
sys.path.append('../server/src')

from nsqrpc.client import NSQClient
from tornado.ioloop import IOLoop
from tornado.gen import coroutine, Return, sleep
from tornado.concurrent import Future

private_key = paramiko.RSAKey.from_private_key_file('fabfile/ssh_key/key_kdjx_nsq')

def objectid2string(id):
	return binascii.hexlify(id)

def string2objectid(s):
	return binascii.unhexlify(s)

@coroutine
def wait():
	yield sleep(1)

ACCOUNT_LIST = [

'5e8dd312bb0b0862f40984bc',
'5e8dcfd1bb0b0862f409825f',
'5e8ddbc4bb0b08062bb90f64',
'5e8dcfd1bb0b0862f4098265',
'5e8dd309bb0b0862f40984b2',
'5e8dd5bbbb0b086fd363e9af',
'5e8dd59ebb0b086fd363e997',
'5e8ddbc4bb0b08062bb90f65',
'5e8ddbc4bb0b08062bb90f62',
'5e8d65eabb0b086a09e73895',
'5e8d8647bb0b0817cfecf2c7',
'5e8d8ca8bb0b0817cfecf9a9',
'5e8d697abb0b086a09e73c6d',
'5e8d68d3bb0b086a09e73bbc',
'5e8dcfd1bb0b0862f4098266',
'5e8d8cb5bb0b0817cfecf9ba',
'5e8dcfe4bb0b0862f409827c',
'5e8d68c7bb0b086a09e73ba8',
'5e8dd2ebbb0b0862f4098498',
'5e8ddbc4bb0b08062bb90f60',
'5e8d403abb0b084fe35492a6',
'5e8dcfd1bb0b0862f4098262',
'5e8d6607bb0b086a09e738a9',
'5e8dd312bb0b0862f40984b8',
'5e8d697bbb0b086a09e73c70',
'5e8dd312bb0b0862f40984bc',
'5e8dcfd1bb0b0862f409825f',
'5e8dcfd1bb0b0862f4098265',
'5e8dd5bbbb0b086fd363e9af',
'5e8d6608bb0b086a09e738ad',
'5e8ddbc4bb0b08062bb90f65',
'5e8dd59ebb0b086fd363e997',
'5e8ddbc4bb0b08062bb90f62',
'5e8d65eabb0b086a09e73895',
'5e8d8ca8bb0b0817cfecf9a9',
'5e8d697abb0b086a09e73c6d',
'5e8d8cb5bb0b0817cfecf9ba',
'5e8ddbc4bb0b08062bb90f64',
'5e8d68c7bb0b086a09e73ba8',
'5e8dd2ebbb0b0862f4098498',
'5e8dcfd1bb0b0862f4098262',
'5e8d6607bb0b086a09e738a9',
'5e8dd312bb0b0862f40984bc',
'5e8d68d3bb0b086a09e73bbc',
'5e8dcfe4bb0b0862f409827c',
'5e8dcfd1bb0b0862f4098266',
'5e8dd5bbbb0b086fd363e9af',
'5e8d6608bb0b086a09e738ad',
'5e8dd59ebb0b086fd363e997',
'5e8d65eabb0b086a09e73895',
'5e8d8647bb0b0817cfecf2c7',
'5e8d403abb0b084fe35492a6',
'5e8d8ca8bb0b0817cfecf9a9',
'5e8d697abb0b086a09e73c6d',
'5e8dd312bb0b0862f40984b8',
'5e8dcfd1bb0b0862f409825f',
'5e8d8cb5bb0b0817cfecf9ba',
'5e8d697bbb0b086a09e73c70',
'5e8ddbc4bb0b08062bb90f64',
'5e8ddbc4bb0b08062bb90f65',
'5e8dd309bb0b0862f40984b2',
'5e8ddbc4bb0b08062bb90f62',
'5e8dcfd1bb0b0862f4098265',
'5e8d6607bb0b086a09e738a9',
'5e8d68d3bb0b086a09e73bbc',
'5e8dcfe4bb0b0862f409827c',
'5e8dcfd1bb0b0862f4098266',
'5e8d6608bb0b086a09e738ad',
'5e8dd2ebbb0b0862f4098498',
'5e8d8647bb0b0817cfecf2c7',
'5e8d68c7bb0b086a09e73ba8',
'5e8d403abb0b084fe35492a6',
'5e8dcfd1bb0b0862f4098262',
'5e8dd312bb0b0862f40984b8',
'5e8dcfd1bb0b0862f409825f',
'5e8d697bbb0b086a09e73c70',
'5e8ddbc4bb0b08062bb90f64',
'5e8dd312bb0b0862f40984bc',
'5e8dd5bbbb0b086fd363e9af',
'5e8ddbc4bb0b08062bb90f65',
'5e8dd309bb0b0862f40984b2',
'5e8dd59ebb0b086fd363e997',
'5e8d65eabb0b086a09e73895',
'5e8ddbc4bb0b08062bb90f62',
'5e8dcfd1bb0b0862f4098265',
'5e8d8ca8bb0b0817cfecf9a9',
'5e8d697abb0b086a09e73c6d',
'5e8d68d3bb0b086a09e73bbc',
'5e8dcfe4bb0b0862f409827c',
'5e8dcfd1bb0b0862f4098266',
'5e8dd2ebbb0b0862f4098498',
'5e8d8cb5bb0b0817cfecf9ba',
'5e8d8647bb0b0817cfecf2c7',
'5e8d68c7bb0b086a09e73ba8',
'5e8d403abb0b084fe35492a6',
'5e8dcfd1bb0b0862f4098262',
'5e8dd312bb0b0862f40984b8',
'5e8dcfd1bb0b0862f409825f',
'5e8d697bbb0b086a09e73c70',
'5e8dd312bb0b0862f40984bc',
'5e8dd5bbbb0b086fd363e9af',
'5e8d6607bb0b086a09e738a9',
'5e8ddbc4bb0b08062bb90f65',
'5e8dd309bb0b0862f40984b2',
'5e8dd59ebb0b086fd363e997',
'5e8d65eabb0b086a09e73895',
'5e8ddbc4bb0b08062bb90f62',
'5e8dcfd1bb0b0862f4098265',
'5e8d8ca8bb0b0817cfecf9a9',
'5e8d697abb0b086a09e73c6d',
'5e8d6608bb0b086a09e738ad',
'5e8ddbc4bb0b08062bb90f64',
'5e8dd2ebbb0b0862f4098498',
'5e8d8cb5bb0b0817cfecf9ba',
'5e8d68c7bb0b086a09e73ba8',
'5e8dcfd1bb0b0862f4098262',
'5e8dcfd1bb0b0862f4098266',
'5e8d68d3bb0b086a09e73bbc',
'5e8dcfe4bb0b0862f409827c',
'5e8dd312bb0b0862f40984bc',
'5e8d6607bb0b086a09e738a9',
'5e8dd5bbbb0b086fd363e9af',
'5e8dd59ebb0b086fd363e997',
'5e8d65eabb0b086a09e73895',
'5e8d8647bb0b0817cfecf2c7',
'5e8d697abb0b086a09e73c6d',
'5e8d8ca8bb0b0817cfecf9a9',
'5e8d6608bb0b086a09e738ad',
'5e8dd312bb0b0862f40984b8',
'5e8dcfd1bb0b0862f409825f',
'5e8dd580bb0b0862f4098679',
'5e8ddbc4bb0b08062bb90f64',
'5e8d697bbb0b086a09e73c70',
'5e8ddbc4bb0b08062bb90f62',
'5e8ddbc4bb0b08062bb90f65',
'5e8d8cb5bb0b0817cfecf9ba',
'5e8dcfd1bb0b0862f4098265',
'5e8dcfd1bb0b0862f4098266',
'5e8d68d3bb0b086a09e73bbc',
'5e8dcfe4bb0b0862f409827c',
'5e8d6607bb0b086a09e738a9',
'5e8dd309bb0b0862f40984b2',
'5e8dd2ebbb0b0862f4098498',
'5e8dcfd1bb0b0862f4098262',
'5e8d8647bb0b0817cfecf2c7',
'5e8d6608bb0b086a09e738ad',
'5e8dd312bb0b0862f40984b8',
'5e8dcfd1bb0b0862f409825f',
'5e8d68c7bb0b086a09e73ba8',
'5e8dd580bb0b0862f4098679',
'5e8ddbc4bb0b08062bb90f64',
'5e8d697bbb0b086a09e73c70',
'5e8dd59ebb0b086fd363e997',
'5e8dd312bb0b0862f40984bc',
'5e8ddbc4bb0b08062bb90f65',
'5e8ddbc4bb0b08062bb90f62',
'5e8dcfd1bb0b0862f4098265',
'5e8dcfd1bb0b0862f4098266',
'5e8d8ca8bb0b0817cfecf9a9',
'5e8dcfe4bb0b0862f409827c',
'5e8d68d3bb0b086a09e73bbc',
'5e8dd309bb0b0862f40984b2',
'5e8dd2ebbb0b0862f4098498',
'5e8dcfd1bb0b0862f4098262',
'5e8d8647bb0b0817cfecf2c7',
'5e8dcfd1bb0b0862f409825f',
'5e8dd312bb0b0862f40984b8',
'5e8d68c7bb0b086a09e73ba8',
'5e8dd580bb0b0862f4098679',
'5e8d697bbb0b086a09e73c70',
'5e8dd59ebb0b086fd363e997',
'5e8dd312bb0b0862f40984bc',
'5e8ddbc4bb0b08062bb90f65',
'5e8ddbc4bb0b08062bb90f62',
'5e8dcfd1bb0b0862f4098265',
'5e8d8cb5bb0b0817cfecf9ba',
'5e8d6608bb0b086a09e738ad',
'5e8d8ca8bb0b0817cfecf9a9',
'5e8d6607bb0b086a09e738a9',
'5e8ddbc4bb0b08062bb90f64',
'5e8dd309bb0b0862f40984b2',
'5e8dd2ebbb0b0862f4098498',
'5e8dcfd1bb0b0862f4098262',
'5e8dcfd1bb0b0862f4098266',
'5e8d68c7bb0b086a09e73ba8',
'5e8d68d3bb0b086a09e73bbc',
'5e8dd59ebb0b086fd363e997',
'5e8dd312bb0b0862f40984bc',
'5e8d8cb5bb0b0817cfecf9ba',
'5e8d6608bb0b086a09e738ad',
'5e8d8ca8bb0b0817cfecf9a9',
'5e8d6607bb0b086a09e738a9',
'5e8ddbc4bb0b08062bb90f64',
'5e8ddbc4bb0b08062bb90f65',
'5e8d697bbb0b086a09e73c70',
'5e8ddbc4bb0b08062bb90f62',
'5e8dcfd1bb0b0862f4098265',
'5e8dcfd1bb0b0862f4098266',
'5e8d68d3bb0b086a09e73bbc',
'5e8dd2ebbb0b0862f4098498',
'5e8d8cb5bb0b0817cfecf9ba',
'5e8d6608bb0b086a09e738ad',
'5e8d6607bb0b086a09e738a9',
'5e8ddbc4bb0b08062bb90f64',
'5e8ddbc4bb0b08062bb90f65',
'5e8d697bbb0b086a09e73c70',
'5e8ddbc4bb0b08062bb90f62',
'5e8dcfd1bb0b0862f4098265',
'5e8dd59ebb0b086fd363e997',
'5e8dcfd1bb0b0862f4098266',
'5e8d8ca8bb0b0817cfecf9a9',
'5e8dcfe4bb0b0862f409827c',
'5e8d68d3bb0b086a09e73bbc',
'5e8dd2ebbb0b0862f4098498',
'5e8d68c7bb0b086a09e73ba8',
'5e8ddbc4bb0b08062bb90f65',
'5e8d697bbb0b086a09e73c70',
'5e8ddbc4bb0b08062bb90f62',
'5e8dcfd1bb0b0862f4098265',
'5e8dd581bb0b0862f409867d',
'5e8dd59ebb0b086fd363e997',
'5e8d8ca8bb0b0817cfecf9a9',
'5e8dcfe4bb0b0862f409827c',
'5e8d8cb5bb0b0817cfecf9ba',
'5e8ddbc4bb0b08062bb90f64',
'5e8d6607bb0b086a09e738a9',
'5e8dd2ebbb0b0862f4098498',
'5e8d68c7bb0b086a09e73ba8',
'5e8d6608bb0b086a09e738ad',
'5e8dcfd1bb0b0862f4098266',
'5e8d68d3bb0b086a09e73bbc',
'5e8dd581bb0b0862f409867d',
'5e8dd59ebb0b086fd363e997',
'5e8d8c9fbb0b0817cfecf99d',
'5e8d8ca8bb0b0817cfecf9a9',
'5e8dcfe4bb0b0862f409827c',
'5e8d8cb5bb0b0817cfecf9ba',
'5e8ddbc4bb0b08062bb90f65',
'5e8ddbc4bb0b08062bb90f64',
'5e8d6607bb0b086a09e738a9',
'5e8d68c7bb0b086a09e73ba8',
'5e8d6608bb0b086a09e738ad',
'5e8dcfd1bb0b0862f4098265',
'5e8ddbc4bb0b08062bb90f62',
'5e8dcfd1bb0b0862f4098266',
'5e8ddbb5bb0b086fd363edcc',
'5e8d68d3bb0b086a09e73bbc',
'5e8dd581bb0b0862f409867d',
'5e8dd2ebbb0b0862f4098498',
'5e8d3938bb0b084fe3548b60',
'5e8d68c7bb0b086a09e73ba8',
'5e8d6609bb0b086a09e738b2',
'5e8d8c9fbb0b0817cfecf99d',
'5e8d8cb5bb0b0817cfecf9ba',
'5e8ddbc4bb0b08062bb90f65',
'5e8ddbc4bb0b08062bb90f64',
'5e8d6607bb0b086a09e738a9',
'5e8d6608bb0b086a09e738ad',
'5e8dcfd1bb0b0862f4098265',
'5e8ddbc4bb0b08062bb90f62',
'5e8dd59ebb0b086fd363e997',
'5e8dcfd1bb0b0862f4098266',
'5e8dcfe4bb0b0862f409827c',
'5e8d65ebbb0b086a09e73896',
'5e8ddbb5bb0b086fd363edcc',
'5e8d8ca8bb0b0817cfecf9a9',
'5e8d68d3bb0b086a09e73bbc',
'5e8dd2ebbb0b0862f4098498',
'5e8d3938bb0b084fe3548b60',
'5e8d68c7bb0b086a09e73ba8',
'5e8d68c7bb0b086a09e73ba8',
'5e8d6609bb0b086a09e738b2',
'5e8d8c9fbb0b0817cfecf99d',
'5e8ddbc4bb0b08062bb90f65',
'5e8dcfd1bb0b0862f4098265',
'5e8dd581bb0b0862f409867d',
'5e8ddbc4bb0b08062bb90f62',
'5e8dd59ebb0b086fd363e997',
'5e8dcfe4bb0b0862f409827c',
'5e8d65ebbb0b086a09e73896',
'5e8ddbb5bb0b086fd363edcc',
'5e8ddbc4bb0b08062bb90f60',
'5e8d8cb5bb0b0817cfecf9ba',
'5e8d8ca8bb0b0817cfecf9a9',

]


def do():
	with SSHTunnelForwarder(
		('172.81.227.66', 22),
		ssh_pkey = private_key,
		# ssh_password = "password"
		ssh_username = 'root',
		remote_bind_address= ('172.16.2.14', 4150),
	) as server:
		CNNSQDefs = {
			'reader': {
				'nsqd_tcp_addresses': '127.0.0.1:%s' % server.local_bind_port,
				'output_buffer_timeout': 25, # default 250ms
			},
			'writer': {
				'nsqd_tcp_addresses': '127.0.0.1:%s' % server.local_bind_port,
			},
		}

		client = NSQClient('walle.cn.2', CNNSQDefs['writer'], readerdefs=CNNSQDefs['reader'])
		IOLoop.current().run_sync(wait)

		# resp = client.call('AccountQuery', string2objectid('5e8dcfd1bb0b0862f409825f'), service_id='accountdb.cn.1')
		# resp = client.call('AccountQueryByName', 'tc_qd_10012141', service_id='accountdb.cn.1')
		# print resp

		for account in ACCOUNT_LIST:
			print account
			resp = client.call('AccountDisable', string2objectid(account), True, service_id='accountdb.cn.1')
			print resp

		client.close()


if __name__ == "__main__":
	do()
